package cn.gov.pbc.gd.credit.log.service.login.impl;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liu.jbase.PageListVo;
import com.liu.jbase.ResultVo;
import com.liu.tools.LogUtil;

import cn.gov.pbc.gd.credit.api.log.ILogLoginApi;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.CommonConstant;
import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.log.service.login.ILogLoginService;
import cn.gov.pbc.gd.credit.vo.log.LogLoginVo;


@Service
public class LogLoginServiceImpl implements ILogLoginService{

	private static Logger logger = LoggerFactory.getLogger(LogLoginServiceImpl.class);
	
	@Autowired
	private ILogLoginApi logLoginApi;
	
	@Autowired
	private IRedisService redisService;
	
	
	@Override
	public ResultVo<PageListVo<LogLoginVo>> page(LogLoginVo vo, HttpServletRequest request) {
		
		//Query query = new Query(Criteria.where("operatorId").is(vo.getLoginIp()).and("cname").is("zcy1"));
		
        UserModel userModel = UserUtil.getUserModel(vo.getSid(), redisService);
		
		boolean flag = false;

		if(UserUtil.checkRoleType(userModel.getRoleList(), CommonConstant.ROLE_BUSI_ADMIN)){//业务管理员
			flag = true;
		}
		
		if(! flag && UserUtil.checkRoleType(userModel.getRoleList(), CommonConstant.ROLE_OPRTATOR)){//查询员
			vo.setAccount(userModel.getAccount());
		}
		
		PageListVo<LogLoginVo> pageListVo = new PageListVo<>();
		pageListVo.setCurrPage(vo.getCurPage());
		pageListVo.setPageSize(vo.getPageSize());

		ResultVo<PageListVo<LogLoginVo>> resultVo = new ResultVo<>();
		resultVo.setCode(CodeConstant.SUCCESS);
		
		try {
			PageListVo<LogLoginVo> pageList = logLoginApi.getPage(vo, pageListVo);
			resultVo.setData(pageList);
		} catch (Exception e) {
			logger.error(LogUtil.msg("LogLoginServiceImpl:page", "查询登录日志分页异常"), e);
			resultVo.setCode(CodeConstant.EXCEPTION);			
		}
		
		return resultVo;
	}

	
}
